package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.domain.vo.VoiceVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.VoiceApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;

@Slf4j
@Service
public class VoiceService {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private VoiceApi voiceApi;

    @Autowired
    private VoiceMqService voiceMqService;

    @Autowired
    private RedisTemplate redisTemplate;

    public ResponseEntity<Object> saveVoice(MultipartFile soundFile) throws IOException {
        //2. 上传语音到fastdfs上
        //2.1获取文件名
        String fileName = soundFile.getOriginalFilename();
        //2.2获取后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        //2.3 上传
        StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), suffix, null);
        System.out.println(storePath);
        //2.4 获取完整的weburl访问地址
        String voiceUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();

        System.out.println(voiceUrl);
        //3. 创建voice对象并保存
        Voice voice = new Voice();
        //设置默认值
        voice.setId(ObjectId.get());
        voice.setCreated(System.currentTimeMillis());

        UserInfo userInfo = userInfoApi.findById(UserHolder.getUserId());

        voice.setUserId(UserHolder.getUserId());
        if (userInfo!=null){
            voice.setAge(userInfo.getAge());
            voice.setGender(userInfo.getGender());
        }

        voice.setSoundUrl(voiceUrl);
        voice.setState(0);

        voiceApi.save(voice);

        /*try {
            // 发送mq消息
           voiceMqService.voiceMsg(voice.getId().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        return ResponseEntity.ok(null);
    }

    public ResponseEntity<Object> findByState() {

        //查找权限
        String key="voice_remainingTimes_"+UserHolder.getUserId();
        Integer remainingTimes = (Integer)redisTemplate.opsForValue().get(key);

        if (remainingTimes==null){
           redisTemplate.opsForValue().set(key,10, Duration.ofDays(1));
           remainingTimes=10;
        }
        //如果次数小于等于0
        if (remainingTimes<=0){
            return ResponseEntity.ok(null);
        }


        Voice voice = voiceApi.findByState(UserHolder.getUserId());

        if (voice==null){
            return ResponseEntity.ok(null);
        }


        //查出voice的数据封装vo
        VoiceVo voiceVo = new VoiceVo();

        voiceVo.setId(voice.getUserId());
        UserInfo userInfo = userInfoApi.findById(voice.getUserId());

        voiceVo.setAvatar(userInfo.getAvatar());
        voiceVo.setNickname(userInfo.getNickname());
        voiceVo.setGender(voice.getGender());
        voiceVo.setAge(voice.getAge());
        voiceVo.setSoundUrl(voice.getSoundUrl());




        //将voice的state置为1
        voice.setState(1);
        voiceApi.setVoiceState(voice);

        remainingTimes--;
        voiceVo.setRemainingTimes(remainingTimes);
        redisTemplate.opsForValue().set(key,remainingTimes);

        return ResponseEntity.ok(voiceVo);
    }
}
